using System;
using System.Collections.Generic;
using System.Text;

namespace SymAutomate.JobFile
{
    /// <summary>
    /// Error Numbers 300-319; Reference file ErrorNumbers.txt for more information.
    /// 300 Error: Required argument 'jobfilename' missing. Use /h switch for help.
    /// 301 Error: Required argument 'jobfileruntimeoutinsec' out of range. Valid range 1-3600.
    /// 302 Error: Invaid value for 'jobfielrunasync'. %ExceptionMessage%.
    /// 303 Error: Invalid value for 'jobfilebatchqueue'. Value values are 0,1,2,3 or 99
    /// 304 Error: Required argument 'jobfilebatchqueue' missing or could not be parsed.  %ExceptionMessage%.
    /// 305 Error: Required argument 'jobfileruntimeoutinsec' missing or could not be parsed. %ExceptionMessage%.
    /// </summary>
    class Parameters : ParametersBase
    {
        #region Class Variables and Access Methods
        private int __JobFileRunTimeoutInSec;
        private string __JobFileName;
        private int __JobFileBatchQueue;
        private bool __JobFileRunAsync;
        private string __JobFileScheduleDate;
        private string __JobFileScheduleTime;
        private string __JobFileExpectedSystemDate;
        private string __JobFileExpectedPreviousSystemDate;
        private JobFileParameter __JobFileParameter;

        public int JobFileRunTimeoutInSec { get { return __JobFileRunTimeoutInSec; } }
        public string JobFileName { get { return __JobFileName; } }
        public int JobFileBatchQueue { get { return __JobFileBatchQueue; } }
        public bool JobFileRunAsync { get { return __JobFileRunAsync; } }
        public string JobFileScheduleDate { get { return __JobFileScheduleDate; } }
        public string JobFileScheduleTime { get { return __JobFileScheduleTime; } }
        public string JobFileExpectedSystemDate { get { return __JobFileExpectedSystemDate; } }
        public string JobFileExpectedPreviousSystemDate { get { return __JobFileExpectedPreviousSystemDate; } }
        public JobFileParameter JobFileParameter { get { return __JobFileParameter; } }
        #endregion

        public Parameters(ConsoleArguments _ConsoleArguments, List<ConsoleHelp> _ConsoleHelpList)
            :base (_ConsoleArguments, _ConsoleHelpList)
        {
            //defaults
            __JobFileRunAsync = false;
            __JobFileBatchQueue = 0;
            __JobFileScheduleDate = string.Empty;
            __JobFileScheduleTime = string.Empty;
            __JobFileExpectedSystemDate = string.Empty;
            __JobFileExpectedPreviousSystemDate = string.Empty;
            __JobFileParameter = new JobFileParameter();

            //read from config file
            try
            {
                __JobFileRunTimeoutInSec = GlobalFunctions.GetIntValue(System.Configuration.ConfigurationManager.AppSettings["jobfileruntimeoutinsec"].ToString());
            }
            catch (Exception _ArgumentException)
            {
                throw new ReturnException("305 Error: Required argument 'jobfileruntimeoutinsec' missing or could not be parsed. " + _ArgumentException.Message + ".");
            }
            try
            {
                __JobFileBatchQueue = GlobalFunctions.GetIntValue(System.Configuration.ConfigurationManager.AppSettings["jobfilebatchqueue"].ToString());
            }
            catch (Exception _ArgumentException)
            {
                throw new ReturnException("304 Error: Required argument 'jobfilebatchqueue' missing or could not be parsed. " + _ArgumentException.Message + ".");
            }

            //save from command line arguments
            try
            {
                if (_ConsoleArguments.ArgumentCollection.Contains("jobfileruntimeoutinsec")) __JobFileRunTimeoutInSec = GlobalFunctions.GetIntValue(_ConsoleArguments.ArgumentCollection["jobfileruntimeoutinsec"].ToString());
            }
            catch (ArgumentException _ArgumentException)
            {
                throw new ReturnException("305 Error: Required argument 'jobfileruntimeoutinsec' missing or could not be parsed. " + _ArgumentException.Message + ".");
            }
            try
            {
                if (_ConsoleArguments.ArgumentCollection.Contains("jobfilebatchqueue")) __JobFileBatchQueue = GlobalFunctions.GetIntValue(_ConsoleArguments.ArgumentCollection["jobfilebatchqueue"].ToString());
            }
            catch (ArgumentException _ArgumentException)
            {
                throw new ReturnException("304 Error: Required argument 'jobfilebatchqueue' missing or could not be parsed. " + _ArgumentException.Message + ".");
            }
            if (_ConsoleArguments.ArgumentCollection.Contains("jobfilename")) __JobFileName = _ConsoleArguments.ArgumentCollection["jobfilename"].ToString();
            try
            {
                if (_ConsoleArguments.ArgumentCollection.Contains("jobfilerunasync")) __JobFileRunAsync = GlobalFunctions.GetBooleanValue(_ConsoleArguments.ArgumentCollection["jobfilerunasync"].ToString());
            }
            catch (ArgumentException _ArgumentException)
            {
                throw new ReturnException("302 Error: Invaid value for 'jobfielrunasync'. " + _ArgumentException.Message + ".");
            }
            if (_ConsoleArguments.ArgumentCollection.Contains("jobfilescheduledate")) __JobFileScheduleDate = _ConsoleArguments.ArgumentCollection["jobfilescheduledate"].ToString();
            if (_ConsoleArguments.ArgumentCollection.Contains("jobfilescheduletime")) __JobFileScheduleTime = _ConsoleArguments.ArgumentCollection["jobfilescheduletime"].ToString();
            if (_ConsoleArguments.ArgumentCollection.Contains("jobfileexpectedsystemdate")) __JobFileExpectedSystemDate = _ConsoleArguments.ArgumentCollection["jobfileexpectedsystemdate"].ToString();
            if (_ConsoleArguments.ArgumentCollection.Contains("jobfileexpectedprevioussystemdate")) __JobFileExpectedPreviousSystemDate = _ConsoleArguments.ArgumentCollection["jobfileexpectedprevioussystemdate"].ToString();
            if (_ConsoleArguments.ArgumentCollection.Contains("jobfileparam")) __JobFileParameter = new JobFileParameter(_ConsoleArguments.ArgumentCollection["jobfileparam"].ToString());

            //perform argument error checking
            if (string.IsNullOrEmpty(__JobFileName))
                throw new ReturnException("300 Error: Required argument 'jobfilename' missing. Use /h switch for help.");
            if (__JobFileRunTimeoutInSec <= 0 || __JobFileRunTimeoutInSec > 3600)
                throw new ReturnException("301 Error: Required argument 'jobfileruntimeoutinsec' out of range. Valid range 1-3600.");
            if (__JobFileBatchQueue != 99 && (__JobFileBatchQueue < 0 || __JobFileBatchQueue > 3))
                throw new ReturnException("303 Error: Invalid value for 'jobfilebatchqueue'. Valid values are 0,1,2,3 or 99");
        }
    }
}
